package reduce.fz.com.reduce.activity;

import android.app.Service;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.fastjson.TypeReference;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.fragment.TeamDetailFragment;
import reduce.fz.com.reduce.fragment.UserDetailFragment;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class UserDetailActivity extends BaseToolBarActivity implements View.OnClickListener {
    private User user;
    private Long uid;
    private FrameLayout userContainer;
    private UserDetailFragment fragment;
    private UserService userService = ServiceBuilder.builderUserService(null);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        setToolbarTitle("我的");
        uid = (Long) getIntent().getLongExtra("uid", 0L);
        loadUser(uid);
        initWidget();
        initListener();


    }

    public void attachFragment() {
        fragment = UserDetailFragment.create(user);
        getSupportFragmentManager().beginTransaction().replace(R.id.user_detail_container, fragment).commit();
    }

    /**
     * 加载用户
     *
     * @param uid
     */
    public void loadUser(Long uid) {
        final LoadingDialog dialog = new LoadingDialog(this);
        dialog.show();
        userService.getUser(uid, new AbResponseListener<User>(new TypeReference<Result<User>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<User> data) {
                dialog.dismiss();
                user = data.getData();
                attachFragment();

            }
        });
    }


    @Override
    public void initWidget() {
        userContainer = (FrameLayout) findViewById(R.id.user_detail_container);
    }

    @Override
    public void initListener() {

    }


    @Override
    public void onClick(View v) {

    }
}
